<template>
	<view class="addcard">
		<u-navbar
		          title="添加健康卡"
		          :autoBack="true"
		      >
		      </u-navbar>
		<view class="title">
			请登记您正确的实名信息进行预约挂号，否则就诊当日将无法就诊。信息一经注册，不可修改，请核对您的登记信息无误后，点击“确定”提交。
		</view>
		<view class="userinfo">
			<view class="useritem">
				<view class="useriteml">
					<span style="color: red;">*</span>姓名
				</view>
				<view class="useritemr">
					<input v-model="name" type="text" placeholder="请输入姓名">
				</view>
			</view>
			<view class="useritem">
				<view class="useriteml">
					<span style="color: red;">*</span>证件类型
				</view>
				<view class="useritemr" @click="choosecardtype()" style="display: flex;">
					{{cardtype}}
					<u-icon color="#cccccc" name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="useritem">
				<view class="useriteml">
					<span style="color: red;">*</span>证件号码
				</view>
				<view class="useritemr">
					<input  v-model="card" type="text" placeholder="请输入证件号码">
				</view>
			</view>
			<view class="useritem">
				<view class="useriteml">
					<span style="color: red;">*</span>年龄
				</view>
				<view class="useritemr">
					<input v-model="age" type="text" placeholder="请输入年龄">
				</view>
			</view>
			<view class="useritem">
				<view class="useriteml">
					<span style="color: red;">*</span>性别
				</view>
				<view class="useritemr" style="display: flex;" v-if="sex" @click="choosesex()">
					{{sex}}
					<u-icon color="#cccccc" name="arrow-right"></u-icon>
				</view>
				<view class="useritemr" style="display: flex;color: #ccc;" v-if="!sex" @click="choosesex()">
					 <u-radio-group
					    v-model="valuesex"
					  placement="row"
					    @change="groupChange"
					  >
					    <u-radio
					      :customStyle="{marginBottom: '8px'}"
					      v-for="(item, index) in radiolist1"
					      :key="index"
					      :label="item.name"
					      :name="item.name"
					      @change="radioChange"
					    >
					    </u-radio>
					  </u-radio-group>
				</view>
			</view>
			
			<view class="useritem">
				<view class="useriteml">
					<span style="color: red;">*</span>民族
				</view>
				<view class="useritemr" @click="chooseminzu()" style="display: flex;">
					{{minzu}}
					<u-icon color="#cccccc" name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<view class="userinfo">
			<view class="useritem">
				<view class="useriteml">
					<span style="color: red;">*</span>手机号码
				</view>
				<view class="useritemr" style="display: flex;">
					<input v-model="phone" type="text" placeholder="请输入手机号"><view class="ma">
					<span v-show="show1" @click="makeCode()">获取验证码</span>
					<span v-show="!show1" class="count">{{count}} s</span>
				</view>
				</view>
			</view>
<view class="useritem">
				<view class="useriteml">
					<span style="color: red;">*</span>验证码
				</view>
				<view class="useritemr">
					<input v-model="code" type="number" placeholder="请输入验证码">
				</view>
			</view>
			<view class="useritem">
				<view class="useriteml">
					详细地址
				</view>
				<view class="useritemr">
					<input v-model="address" type="text" placeholder="请输入详细地址">
				</view>
			</view>

		</view>
		<view class="btn" @click="adduser()">
			确定
		</view>
		<view class="bang" @click="shouquan()">
			一键授权，已有健康卡用户直接绑定>
		</view>
	<!-- 	<u-popup :show="showcardtype" mode="bottom" @close="closepop()">
			<view class="btnlist">
				<view class="btnl" @click="closepoptype()">
					取消
				</view>
				<view class="btnr" @click="confirmpoptype()">
					确定
				</view>
			</view>
			<picker-view :value="value" @change="onChange" class="my-picker">
				<picker-view-column>
					<view v-for="item in cardtypelist">{{item}}</view>
				</picker-view-column>
			</picker-view>

		</u-popup> -->
		<u-popup :show="showw" customStyle="width:700rpx;" :round="10" mode="center" width="80%" :closeable="true" @close="closee" height="" border-radius="10">
			<view class="pop">
				<!-- <view class="poptop">
					<u-navbar :custom-back="cancell" :is-back="true" :title-bold="true" title="健康卡授权"></u-navbar>
				</view> -->
				<view class="cardimg">
					<image src="../../../static/icons/cardicon.png" mode=""></image>
				</view>
				<view class="hosname">
					河南预约挂号平台
				</view>
				<view class="" v-if="showpp">
					<health-card-login encrypt="true" authLogin="false" @authFail="authFail" @authSucess="authSuccess" :authCancel="authCancel"
					 wechatcode="true">
						<view class="foott">
							<view @click="hideComponentLogin()" class="agree">一键登录授权</view>
						</view>
						<view class="shuomingg">
							登录代表同意<span @click="goxieyi()">《用户服务协议与隐私政策》</span>并授权使用您的账号信息以便您统一管理
						</view>
					</health-card-login>
				</view>
			</view>
		</u-popup>
		<u-picker @confirm="onChange" @cancel="cancelcard()" :show="showcardtype" :columns="cardtypelist"></u-picker>

		<u-picker mode="selector" @cancel="cancelminzu()"  @confirm="confirmminzu" :show="showwmin" :default-selector="[0]" :columns="minzulist"></u-picker>
		<u-modal :show="showdian" title="温馨提示" @close="close()" :closeOnClickOverlay="true" :content='content'
			@cancel="cancel()" :showCancelButton="true" @confirm="contihuan()"></u-modal>
	</view>
</template>

<script>
	import homeApi from '@/api/home'
	import homeApii from '@/api/homee'
	export default {
		data() {
			return {
				showwmin:false,
				minzulist: [[
					"汉族",
					"壮族",
					"满族",
					"回族",
					"苗族",
					"维吾尔族",
					"土家族",
					"彝族",
					"蒙古族",
					"藏族",
					"布依族",
					"侗族",
					"瑶族",
					"朝鲜族",
					"白族",
					"哈尼族",
					"哈萨克族",
					"黎族",
					"傣族",
					"畲族",
					"傈僳族",
					"仡佬族",
					"东乡族",
					"高山族",
					"拉祜族",
					"水族",
					"佤族",
					"纳西族",
					"羌族",
					"土族",
					"仫佬族",
					"锡伯族",
					"柯尔克孜族",
					"达斡尔族",
					"景颇族",
					"毛南族",
					"撒拉族",
					"布朗族",
					"塔吉克族",
					"阿昌族",
					"普米族",
					"鄂温克族",
					"怒族",
					"京族",
					"基诺族",
					"德昂族",
					"保安族",
					"俄罗斯族",
					"裕固族",
					"乌孜别克族",
					"门巴族",
					"鄂伦春族",
					"独龙族",
					"塔塔尔族",
					"赫哲族",
					"珞巴族"
				]],
				valuesex:"男",
				value: '01',
				valuee: '0',
				showsex: false,
				showcardtype: false,
				cardtype: "身份证",
				sex: "",
				cardtypelist: [
					["身份证", "港澳居民来往内地通行证","台湾居民来往内地通行证"]
				],
				sexlist: [
					"男", "女"
				],
				phone: "",
				name: "",
				card: "",
				address: "",
				age: "",
				show1: true,
				count: 0,
				timer: null,
				code:"",
				type:"",
				wechatCode:"",
				content:'',
				showdian:false,
				huanphone:'',
				showpp: false,
				showw:false,
				 radiolist1: [{
				            name: '男',
				            disabled: false
				          },
				            {
				              name: '女',
				              disabled: false
				            },
				            
				          ],
				          // u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
						  minzu:"汉族",
						  userinfo:""
						
			}
		},
		onShow() {

		},
		onLoad(option) {
			if(option.type){
				this.type=option.type
				console.log("这会"+this.type)
			}
			let healthCode=uni.getStorageSync("healthCode")
			// if(option.healthCode){
			// 	let healthCode=option.healthCode
			// 	console.log(healthCode,"有了")
			// 	this.getjiankangdetail(healthCode) 
			// }
			if(healthCode){
				 
				this.getjiankangdetail(healthCode) 
			} 
			// this.wechatCode=option.wechatCode  
			// 	 console.log(this.wechatCode,"还在不在")
			// 	 if(this.wechatCode){
					 
			// 	 }else{
			// 		 this.wechatCode=uni.getStorageSync("wechatCode")
			// 		 console.log(this.wechatCode,"111111")   
			// 	 }
			this.login()
		},
		methods: {
			hideComponentLogin() {
				this.showw = false
				uni.showLoading()
			},
			authSuccess(data) {
				uni.hideLoading()
				let res = data.detail
					this.todo(res)
			},
			authCancel() {
				uni.hideLoading()   
			
			},
			// 业务代码（成功获取登录数据）
			todo(res) {
				// 传入参数 wechatcode = true 结果输出 wechatCode ，注意大小写
				// 传入参数 healthcode = true 及健康卡卡号 ecard_no，输出healthCode
				this.wechatCode = res.result.wechatCode 
				if (this.wechatCode) {
					
				} else {
					this.login()
				}
			 
			},
			login() {
				const plugin = requirePlugin('myPlugin')
				// plugin.login(callback, options)
				plugin.login((isok, res) => {
					console.log('plugin.login: ', isok, res)
					if (res.result.type !== 3) {
						// 用户在微信授权过，可直接获取登录信息，处理后续业务
						this.todo(res)
					} else {
						// 用户未授权，需要用户同意授权
						// 显示 healthCardLogin 登录组件，引导用户同意授权
						this.showw = true
						this.showpp = true
					}
				}, {
					wechatcode: true
				})
			},
			cancel() {
				this.showdian = false
			if(this.type){
				if(this.type==1){
					uni.showToast({
						title:"成功",
						icon:"none"
					})
					uni.removeStorageSync("wechatCode")
					// uni.switchTab({
					// 	url:"/pages/mine/mine"
					// })
					let pages = getCurrentPages(); // 当前页面
					let beforePage = pages[pages.length - 2]; // 前一个页面
					uni.navigateBack({
						success: function() {
							beforePage.onLoad(); // 执行前一个页面的onLoad方法
						},
					});	
				}}
				// this.getuserinfo()
			},
			contihuan() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				let token=uni.getStorageSync("token")
				homeApii.newphone({
					userinfoid: this.userinfo.userid,
					token:token,
					phone: this.huanphone
				}).then(res => {
					uni.hideLoading()
					this.showdian =false 
				if(this.type){
					if(this.type==1){
						uni.showToast({
							title:"成功",
							icon:"none"
						})
						uni.removeStorageSync("wechatCode")
						// uni.switchTab({
						// 	url:"/pages/mine/mine"
						// })
						let pages = getCurrentPages(); // 当前页面
						let beforePage = pages[pages.length - 2]; // 前一个页面
						uni.navigateBack({
							success: function() {
								beforePage.onLoad(); // 执行前一个页面的onLoad方法
							},
						});	
					}}
					// if (res.code == 4000) {
					// 	uni.showToast({
					// 		title: "成功"
					// 	})
					// }
				})
			},
			close() {
				this.showdian = false
				if(this.type){
					if(this.type==1){
						uni.showToast({
							title:"成功",
							icon:"none"
						})
						uni.removeStorageSync("wechatCode")
						// uni.switchTab({
						// 	url:"/pages/mine/mine"
						// })
						let pages = getCurrentPages(); // 当前页面
						let beforePage = pages[pages.length - 2]; // 前一个页面
						uni.navigateBack({
							success: function() {
								beforePage.onLoad(); // 执行前一个页面的onLoad方法
							},
						});	
					}}
				// this.getuserinfo()
			},
			getjiankangdetail(healthCode){
				uni.showLoading({title: '加载中',mask:true});
				let token=uni.getStorageSync("token")
				homeApii.getweicard({
					healthCode:healthCode,
					token:token
				}).then(res=>{
					uni.hideLoading()
					if(res.code==4000){
						uni.removeStorageSync("healthCode")
						
						console.log(res)
						this.name=res.data.name
						this.address=res.data.address 
						this.phone=res.data.phone
						this.card=res.data.cardno
						if(res.data.cardtype=="01"){
							this.cardtype="身份证"
						}
						if(res.data.cardtype=="06"){
							this.cardtype="港澳居民来往内地通行证"
						}
						if(res.data.cardtype=="07"){
							this.cardtype="台湾居民来往内地通行证"
						}
						if(res.data.sex=="0"){
							this.valuesex="女"
						}
						if(res.data.sex=="1"){
							this.valuesex="男"
						}
					}else{
						uni.showToast({
							title:res.msg,
							icon:"none"
						})
					}
				})
			},
			cancelminzu(){
				this.showwmin=false
			},
			cancelcard(){
				this.showcardtype=false
			},
			chooseminzu(){
				this.showwmin=true
			},
			confirmminzu(e) {
				console.log(e,1111)
				let index = e.value[0]
				
				this.minzu=index
			
				this.showwmin=false
				// let index = val[0]
				// let minzu = this.minzulist.find((el) => this.minzulist.indexOf(el) == index)
				// this.minzu = minzu
			},
			  groupChange(n) {
			        console.log('groupChange', n);
			      },
			      radioChange(n) {
			        console.log('radioChange', n);
			      },
			shouquan(){
				if(this.type==2){
					uni.navigateTo({
						url: "../bindcard/bindcard?type=2"
					})
				}else{
					uni.setStorageSync("wechatCode",this.wechatCode)
					uni.navigateTo({
						url: "../bindcard/bindcard"
					})
				}
				
			},
			makeCode() {
				// 判断手机号是否正确
				let regPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				let msg = "";
			
				if (this.phone == "") {
					msg = "请输入正确的手机号";
					uni.showToast({
						title: "请输入手机号",
						icon: "none"
					})
				} else if (!regPhone.test(this.phone)) {
					msg = "请输入正确的手机号";
					uni.showToast({
						title: "请输入正确的手机号",
						icon: "none"
					})
				}
				if (msg == "") {
					let token=uni.getStorageSync("token")
					homeApii.getphonecode({
						phone: this.phone,
						token:token
					}).then(res => {
						console.log(res)
						if (res.code == 4000) {
							uni.showToast({
								title: "发送成功",
								icon: "none"
							})
							const TIME_COUNT = 60;
							if (!this.timer) {
								this.count = TIME_COUNT;
								this.show1 = false;
								this.timer = setInterval(() => {
									if (this.count > 0 && this.count <= TIME_COUNT) {
										this.count--;
									} else {
										this.show1 = true;
										clearInterval(this.timer);
										this.timer = null;
									}
								}, 1000)
							}
						} else if(res.code==4001) {
							uni.showToast({
								title: "关联超过三条档案信息,请您更换手机号",
								icon: "none"
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					})
			
				}
			},
			adduser() {
				// console.log(this.gender);
				// 判断手机号是否正确
				let regPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				// 邮箱正则
				var emailReg = /^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/;
				// 通行证正则
				var idCard = /^[H|M]\d[0-9]{7}$/;
			
                     var idcrdd=/^[A-Za-z0-9]{6,10}$/

				// 身份证正则
				var levalard =
				/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
				// 出生证正则
				var birthdayCard = /^[A-Z]{1}\d[0-9]{8}$/;
				var num = /^[0-9]*$/;

				var msg = "";
				if (this.name == "") {
					msg = "请输入姓名";
					uni.showToast({
						title: msg,
						icon: "none"
					})
				}


				if (this.card == "") {
					msg = "请输入证件号";
					uni.showToast({
						title: msg,
						icon: "none"
					})
				}
				// if (this.name == "") {
				// 	msg = "请输入姓名";
				// uni.showToast({
				// 	title: msg,
				// 	icon: "none"
				// })
				// }
				if (this.cardtype == "身份证") {
				

					var idCard = this.card;
					idCard = idCard.toUpperCase(); // 转大写
					let len = idCard.length;
					let city = {
						11: "北京",
						12: "天津",
						13: "河北",
						14: "山西",
						15: "内蒙古",
						21: "辽宁",
						22: "吉林",
						23: "黑龙江 ",
						31: "上海",
						32: "江苏",
						33: "浙江",
				 	34: "安徽",
						35: "福建",
						36: "江西",
						37: "山东",
						41: "河南",
						42: "湖北 ",
						43: "湖南",
				  44: "广东",
						45: "广西",
						46: "海南",
						50: "重庆",
						51: "四川",
						52: "贵州",
						53: "云南",
						54: "西藏 ",
						61: "陕西",
						62: "甘肃",
						63: "青海",
						64: "宁夏",
						65: "新疆",
						71: "台湾",
						81: "香港",
						82: "澳门",
						91: "国外"
					};
					//1. 判断长度
					if (len !== 15 && len !== 18) {
						msg = "请输入正确的证件号";
						uni.showToast({
							title: "请输入正确的证件号",
							icon: "none"
						})
					}
					//2. 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。
					if (!/(^\d{15})|(^̲\d{17}([0-9]|X))/.test(idCard)) {
						msg = "请输入正确的证件号";
						uni.showToast({
							title: "请输入正确的证件号",
							icon: "none"
						})
					}
					//3. 地址码
					if (!city[idCard.substr(0, 2)]) {
						msg = "请输入正确的证件号";
						uni.showToast({
							title: "请输入正确的证件号",
							icon: "none"
						})
					}

					let re;
					//4. 生日
					if (len === 15) {
						re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
						let arrSplit = idCard.match(re);

						//检查生日日期是否正确
						let birth = new Date(
							"19" + arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]
						);
						let isBirth =
							birth.getYear() === Number(arrSplit[2]) &&
							birth.getMonth() + 1 === Number(arrSplit[3]) &&
							birth.getDate() === Number(arrSplit[4]);
						if (!isBirth) {
							msg = "请输入正确的证件号";
							uni.showToast({
								title: "请输入正确的证件号",
								icon: "none"
							})
						}
					} else if (len === 18) {
						re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
				 	let arrSplit = idCard.match(re);

						//检查生日日期是否正确
						let birth = new Date(
							arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]
						);
						let isBirth =
							birth.getFullYear() === Number(arrSplit[2]) &&
							birth.getMonth() + 1 === Number(arrSplit[3]) &&
							birth.getDate() === Number(arrSplit[4]) &&
							birth.getTime() < new Date().getTime();
						if (!isBirth) {
							msg = "请输入正确的证件号";
				 uni.showToast({
				 	title: "请输入正确的证件号",
				 	icon: "none"
				 })
						} else {
							//检验18位身份证的校验码是否正确。
							//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
							let valnum;
							let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
							let arrCh = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
							let nTemp = 0,
								i;
							for (i = 0; i < 17; i++) {
								nTemp += idCard.substr(i, 1) * arrInt[i];
							}
							valnum = arrCh[nTemp % 11];
							if (valnum !== idCard.substr(17, 1)) {
								msg = "请输入正确的证件号";
								uni.showToast({
									title: "请输入正确的证件号",
									icon: "none"
								})
							}
						}
					}
				}
				if (this.cardtype == "出生证") {
					if (!birthdayCard.test(this.card)) {
						msg = "请输入正确的证件号";
						uni.showToast({
							title: "请输入正确的证件号",
							icon: "none"
						})
					}
				}
				if (this.cardtype == "港澳居民来往内地通行证") {
					if (!idCard.test(this.card)) {
						msg = "请输入正确的证件号";
						uni.showToast({
							title: "请输入正确的证件号",
							icon: "none"
						})
					}
				}
				if (this.cardtype == "台湾居民来往内地通行证 ") {
					if (!idcrdd.test(this.card)) {
						msg = "请输入正确的证件号";
						uni.showToast({
							title: "请输入正确的证件号",
							icon: "none"
						})
					}
				}
				// if(!this.sex){
				// 	msg="请选择性别"
				// 	uni.showToast({
				// 		title: "请选择性别",
				// 		icon: "none"
				// 	})
				// }
				
				if (this.age == "") {
					msg = "请输入年龄";
					uni.showToast({
						title: "请输入年龄",
						icon: "none"
					})
				}else{
					if (!num.test(this.age)) {
						msg = "年龄请输入数字";
						uni.showToast({
							title: "年龄请输入数字",
							icon: "none"
						})
					}
				}
				if (this.phone == "") {
					msg = "请输入手机号";
					uni.showToast({
						title: "请输入手机号",
						icon: "none"
					})
				}
				if (!regPhone.test(this.phone)) {
					msg = "请输入正确的手机号";
				uni.showToast({
					title: "请输入正确的手机号",
					icon: "none"
				})
				}
				var cadvalue=""
				if(this.cardtype=="身份证"){
					cadvalue="01"
				}
				if(this.cardtype=="港澳居民来往内地通行证"){
					cadvalue="06"
				}
				if(this.cardtype=="台湾居民来往内地通行证"){
					cadvalue="07"
				}
				let valuesex=""
				if(this.valuesex=="男"){
					valuesex="1"
				}
				if(this.valuesex=="女"){
					valuesex="0"
				}
				
				if (msg == "") {
					uni.showLoading({title: '加载中',mask:true});
					let token=uni.getStorageSync("token")
					homeApii.addjiancard({
						token:token,
						cardtype:cadvalue,
						age:this.age,
						sex:valuesex,
						name:this.name,
						cardno:this.card,
						phone:this.phone,
						phonecode:this.code,
						nation:this.minzu,
						address:this.address,
						wechatCode:this.wechatCode,
						creattype:"creat",
					    healthCode:""
					}).then(res=>{
						uni.hideLoading()
						console.log(res)
						if(res.code==4000){
							if(res.data.flag == false){
								if(this.type){
									if(this.type==1){
										uni.showToast({
											title:"成功",
											icon:"none"
										})
										uni.removeStorageSync("wechatCode")
										// uni.switchTab({
										// 	url:"/pages/mine/mine"
										// })
										setTimeout(function () {
										   let pages = getCurrentPages(); // 当前页面
										   let beforePage = pages[pages.length - 2]; // 前一个页面
										   uni.navigateBack({
										   	success: function() {
										   		beforePage.onLoad(); // 执行前一个页面的onLoad方法
										   	},
										   });	
										}, 2000)
										
									}
								}else{
									uni.showToast({
										title:"成功",
										icon:"none"
									})
									setTimeout(function () {
									 uni.switchTab({
									 	url:"/pages/mine/mine"
									 })
									}, 2000)
									uni.removeStorageSync("wechatCode")
									
								}
								
							}
						
						
						if(res.data.flag == true){
							this.content = "您在该平台已绑定手机号为" + res.data.stockphone + "是否要替换？"
							this.show = true
							this.huanphone=res.data.phone
							this.userinfo=res.data.userinfo
						}	
						}else{
							uni.showToast({
								title:res.msg,
								icon:"none"
							})
						}
					})
			
				}
			},
			check() {
				if (this.cardtype == "身份证") {
					var idCard = this.card
					let _this = this;
					let bbd = ''; // 出生日期
					let sex = ''; // 性别
					let canPass = true;
					idCard = idCard.toUpperCase(); // 转大写
					let len = idCard.length;
					let city = {
						11: "北京",
						12: "天津",
						13: "河北",
						14: "山西",
						15: "内蒙古",
						21: "辽宁",
						22: "吉林",
						23: "黑龙江 ",
						31: "上海",
						32: "江苏",
						33: "浙江",
						34: "安徽",
						35: "福建",
						36: "江西",
						37: "山东",
						41: "河南",
						42: "湖北 ",
						43: "湖南",
						44: "广东",
						45: "广西",
						46: "海南",
						50: "重庆",
						51: "四川",
						52: "贵州",
						53: "云南",
						54: "西藏 ",
						61: "陕西",
						62: "甘肃",
						63: "青海",
						64: "宁夏",
						65: "新疆",
						71: "台湾",
						81: "香港",
						82: "澳门",
						91: "国外"
					};
					//1. 判断长度
					if (len !== 15 && len !== 18) {
						// _this.$toast.error('身份证号长度不正确！');
						uni.showToast({
							title: "请输入正确的身份证号！",
							icon: "none"
						})
						canPass = false;
					}
					//2. 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。
					if (!(/(^\d{15})|(^̲\d{17}([0-9]|X))/.test(idCard))) {

						uni.showToast({
							title: "请输入正确的身份证号",
							icon: "none"
						})
						canPass = false;
					}
					//3. 地址码
					if (!city[idCard.substr(0, 2)]) {

						uni.showToast({
							title: "请输入正确的身份证号",
							icon: "none"
						})
						canPass = false;
					}

					let re;
					//4. 生日
					if (len === 15) {
						re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
						let arrSplit = idCard.match(re);

						//检查生日日期是否正确
						let birth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);
						let isBirth = (birth.getYear() === Number(arrSplit[2])) && ((birth.getMonth() + 1) === Number(
							arrSplit[
								3])) && (
							birth.getDate() === Number(arrSplit[4]));
						if (!isBirth) {
							uni.showToast({
								title: "请输入正确的身份证号",
								icon: "none"
							})

							canPass = false;
						}
						bbd = birth;
					} else if (len === 18) {
						re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
						let arrSplit = idCard.match(re);

						//检查生日日期是否正确
						let birth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
						let isBirth = (birth.getFullYear() === Number(arrSplit[2])) && ((birth.getMonth() + 1) === Number(
								arrSplit[3])) &&
							(birth.getDate() === Number(arrSplit[4]) && (birth.getTime() < new Date().getTime()));
						if (!isBirth) {


							canPass = false;
						} else {
							//检验18位身份证的校验码是否正确。
							//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
							let valnum;
							let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
							let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
							let nTemp = 0,
								i;
							for (i = 0; i < 17; i++) {
								nTemp += idCard.substr(i, 1) * arrInt[i];
							}
							valnum = arrCh[nTemp % 11];
							if (valnum !== idCard.substr(17, 1)) {
								// uni.showToast({
								// 	title: "身份证号校验码不正确！",
								// 	icon: "none"
								// })

								canPass = false;
							}
						}
						bbd = birth;
					}
					// 如果身份验证通过，计算出生年月，性别
					if (canPass == true) {
						bbd = bbd.getFullYear() + '-' + Number(bbd.getMonth() + 1) + '-' + bbd.getDate();
						let arr_fix = bbd.split('-');
						if (arr_fix[1].length == 1) {
							arr_fix[1] = '0' + arr_fix[1];
						}
						if (arr_fix[2].length == 1) {
							arr_fix[2] = '0' + arr_fix[2];
						}
						bbd = arr_fix[0] + '-' + arr_fix[1] + '-' + arr_fix[2];
						// this.age = this.getAge(bbd)
						console.log(this.age, "111")
						// bbd = arr_fix[0] + '-' + arr_fix[1];
						if (idCard.length == 18) {
							sex = idCard.charAt(idCard.length - 2)
						} else {
							sex = idCard.charAt(idCard.length - 1)
						}
						if (sex % 2 == 1) {
							sex = '男';
							this.sexvalue = 1
							this.sex = "男"
						} else {
							sex = '女';
							this.sexvalue = 2
							this.sex = "女"
						}

					}


				}

			},
			getAge(strBirthday) {
				var returnAge;
				var strBirthdayArr = strBirthday.split("-");
				var birthYear = strBirthdayArr[0];
				var birthMonth = strBirthdayArr[1];
				var birthDay = strBirthdayArr[2];

				let d = new Date();
				var nowYear = d.getFullYear();
				var nowMonth = d.getMonth() + 1;
				var nowDay = d.getDate();

				if (nowYear == birthYear) {
					returnAge = 0; //同年 则为0岁
				} else {
					var ageDiff = nowYear - birthYear; //年之差
			  if (ageDiff > 0) {
						if (nowMonth == birthMonth) {
							var dayDiff = nowDay - birthDay; //日之差
							if (dayDiff < 0) {
								returnAge = ageDiff - 1;
							} else {
								returnAge = ageDiff;
							}
						} else {
							var monthDiff = nowMonth - birthMonth; //月之差
							if (monthDiff < 0) {
								returnAge = ageDiff - 1;
							} else {
								returnAge = ageDiff;
							}
						}
					} else {
						returnAge = -1; //出生日期不能大于今天
					}
				}
				return returnAge;
			},
			choosesex() {
				this.showsex = true
				if(this.sex=='男'){
					this.valuee="0"
				}
				if(this.sex=='女'){
					this.valuee="1"
				}
			},
			choosecardtype() {
				this.showcardtype = true
			// 	if(this.cardtype=='身份证'){
			// 		this.value="01"
			// 	}
			// if(this.cardtype=='台湾居民来往内地通行证'){
			// 	this.value="06"
			// }
			// if(this.cardtype=='港澳居民来往内地通行证'){
			// 	this.value="07"
			// }
			},
			closepoptype() {
				this.showcardtype = false
			},
			confirmpoptype() {
				this.showcardtype = false
			},
			closepopsex() {
				this.showsex = false
			},
			confirmpopsex() {
				this.showsex = false
				if (this.sex == "") {
					this.sex = "男"
					this.valuee="0"
				}
				
			},
			onChange(e) {
				console.log(e,1111)
				let index = e.value[0]
				this.value = index
				this.cardtype=this.value
				// console.log(this.value,"11111")
				// let cardt = this.cardtypelist.find((el) => this.cardtypelist.indexOf(el) == index)
				// this.cardtype = cardt
				this.showcardtype=false
			},
			onChangee(e) {
				let index = e.detail.value[0]
				this.valuee = index
				let cardt = this.sexlist.find((el) => this.sexlist.indexOf(el) == index)
				this.sex = cardt
			},
			closepop() {
				this.showcardtype = false
			},
			closepopp() {
				this.showsex = false
			}

		}
	}
</script>

<style lang="scss" scoped>
	.addcard {
		background-color: #F5F6F8;
		padding-bottom: 100rpx;
		overflow: hidden;
		padding-top: 180rpx;
		.bang{
			width:100%;
			text-align:center;
			color:#3c9cff;
			font-size: 24rpx;
			margin-top:30rpx;
		}
.ma {
					width: 160rpx;
					height: 60rpx;
					border: 1rpx solid #3669F8;
					font-size: 22rpx;
					text-align: center;
					line-height: 60rpx;
					margin-top: 20rpx;
					border-radius: 10rpx;
					color:  #3669F8;
					margin-left: 10rpx;
				}
		.btnlist {
			width: 95%;
			margin: 0 auto;
			display: flex;
			justify-content: space-between;
			line-height: 100rpx;
			font-size: 30rpx;

			.btnl {
				color: #888;
			}

			.btnr {
				color: #4581f7;
			}
		}

		.btn {
			width: 93%;
			margin: 0 auto;
			margin-top: 80rpx;
			border-radius: 15rpx;
			line-height: 80rpx;
			border-radius: 40rpx;
			background-color: #4581f7;
			color: #fff;
			text-align: center;

		}

		.title {
			width: 93%;
			margin: 0 auto;
			margin-top: 20rpx;
			font-size: 28rpx;
			line-height: 40rpx;
		}

		.userinfo {
			width: 93%;
			margin: 0 auto;
			margin-top: 20rpx;
			border-radius: 15rpx;
			background-color: #fff;

			.useritem {
				height: 100rpx;
				border-bottom: 1rpx solid #f7f7f7;
				display: flex;
				justify-content: space-between;
				font-size: 28rpx;
				line-height: 100rpx;
				width: 95%;
				margin: 0 auto;

				.useriteml {
					width: 150rpx;
					color: #888;
				}

				.useritemr {
					input {
						border: none;
						font-size: 28rpx;
						text-align: right;
						// border: 1rpx solid #000;
						margin-top: 25rpx;
					}
				}
			}
		}


	}
</style>
